/*
 * ========================================================
 * Copyright(c) 2014 杭州龙骞科技-版权所有
 * ========================================================
 * 本软件由杭州龙骞科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * http://www.hzdracom.com/
 * ========================================================
 */
package com.shwread.android.ui.page.chat;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.netease.nim.demo.NimCache;
import com.netease.nim.demo.common.adapter.TAdapter;
import com.netease.nim.demo.common.adapter.TAdapterDelegate;
import com.netease.nim.demo.common.adapter.TViewHolder;
import com.netease.nim.demo.common.ui.dialog.CustomAlertDialog;
import com.netease.nim.demo.common.ui.dialog.CustomAlertDialog.onSeparateItemClickListener;
import com.netease.nim.demo.common.ui.listview.ListViewUtil;
import com.netease.nim.demo.contact.cache.ContactDataCache;
import com.netease.nim.demo.contact.cache.TeamDataCache;
import com.netease.nim.demo.contact.cache.TeamMemberInfoCache;
import com.netease.nim.demo.contact.model.Buddy;
import com.netease.nim.demo.contact.protocol.ContactHttpClient;
import com.netease.nim.demo.contact.protocol.IContactHttpCallback;
import com.netease.nim.demo.main.helper.MessageHelper;
import com.netease.nim.demo.main.reminder.ReminderManager;
import com.netease.nim.demo.main.viewholder.CommonRecentViewHolder;
import com.netease.nim.demo.main.viewholder.IMRecentViewHolder;
import com.netease.nim.demo.main.viewholder.RecentViewHolder;
import com.netease.nim.demo.main.viewholder.TeamRecentViewHolder;
import com.netease.nim.demo.session.activity.P2PMessageActivity;
import com.netease.nim.demo.session.activity.TeamMessageActivity;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.auth.ClientType;
import com.netease.nimlib.sdk.auth.OnlineClient;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.netease.nimlib.sdk.team.TeamService;
import com.netease.nimlib.sdk.team.model.DismissAttachment;
import com.netease.nimlib.sdk.team.model.Team;
import com.netease.nimlib.sdk.team.model.TeamMember;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000152.R;
import com.shwread.android.ui.page.AbstractUIPage;
import com.shwread.http.ActionHelp;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.http.util.Logger;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import base.tina.core.task.infc.ITaskResult;
import logic.bean.TeamMemberX;
import logic.util.Utils;


/**
 * @Project： qysw_1.1.6_association
 * @Title：SessionPage
 * @Description：
 * @Author：wayne
 * @Date：2015年11月27日上午12:10:30
 */
public class SessionPage
        extends
        AbstractUIPage<MainActivity>
        implements
        TAdapterDelegate
{
	public static SessionPage   instance;
	
	public static final long    RECENT_TAG_STICKY = 1;            // 联系人置顶tag
	                                                               
	// view
	private ListView            listView;
	
	private View                emptyBg;
	
	private View                notifyBar;
	
	private TextView            emptyHint;
	
	// data
	private List<RecentContact> items;
	
	private TAdapter            adapter;
	
	private boolean             msgLoaded         = false;
	
	private Handler             handler           = new Handler();
	
	public static int           isFirstIn         = 0;
	
	public SessionPage(MainActivity t) {
		super(t);
	}
	
	public static SessionPage getInstance(MainActivity t) {
		if (instance == null) instance = new SessionPage(t);
		return instance;
	}
	
	@Override
	public <E extends Activity> View createView(E arg0, int arg1) {
		if (null != curMyView) return curMyView;
		curMyView = bActivity.getLayoutInflater().inflate(R.layout.messages, null);
		if (isFirstIn == 2)
		{
			msgLoaded = false;
		}
		initViews();
		initMessageList();
		requestMessages(true);
		registerObservers(true);
		return curMyView;
	}
	
	@Override
	public void dispose() {
		super.dispose();
		//		msgLoaded = false;
		requestMessages(false);
		registerObservers(false);
	}
	
	private void initViews() {
		listView = (ListView) this.getView().findViewById(R.id.lvMessages);
		emptyBg = getView().findViewById(R.id.emptyBg);
		notifyBar = getView().findViewById(R.id.status_notify_bar);
		notifyBar.setVisibility(View.GONE);
		emptyHint = (TextView) getView().findViewById(R.id.message_list_empty_hint);
	}
	
	/**
	 * ********************** 收消息，处理状态变化 ************************
	 */
	private void registerObservers(boolean register) {
		MsgServiceObserve service = NIMClient.getService(MsgServiceObserve.class);
		service.observeRecentContact(messageObserver, register);
		service.observeMsgStatus(statusObserver, register);
		service.observeRecentContactDeleted(deleteObserver, register);
		NIMClient.getService(AuthServiceObserver.class).observeOtherClients(clientsObserver, register);
		registerTeamUpdateObserver(register);
		registerTeamMemberUpdateObserver(register);
		registerBuddyUpdateObserver(register);
	}
	
	/**
	 * 注册群信息&群成员更新监听
	 */
	private void registerTeamUpdateObserver(boolean register) {
		if (register)
		{
			TeamDataCache.getInstance().registerTeamDataChangedObserver(teamDataChangedObserver);
		}
		else
		{
			TeamDataCache.getInstance().unregisterTeamDataChangedObserver(teamDataChangedObserver);
		}
	}
	
	private void registerTeamMemberUpdateObserver(boolean register) {
		if (register)
		{
			TeamDataCache.getInstance().registerTeamMemberDataChangedObserver(teamMemberDataChangedObserver);
		}
		else
		{
			TeamDataCache.getInstance().unregisterTeamMemberDataChangedObserver(teamMemberDataChangedObserver);
		}
	}
	
	/**
	 * 注册好友信息更新监听
	 */
	private void registerBuddyUpdateObserver(boolean register) {
		if (register)
		{
			ContactDataCache.getInstance().registerBuddyDataChangedObserver(buddyDataChangedObserver);
		}
		else
		{
			ContactDataCache.getInstance().unregisterBuddyDataChangedObserver(buddyDataChangedObserver);
		}
	}
	
	Observer<List<RecentContact>>               messageObserver               = new Observer<List<RecentContact>>()
	                                                                          {
		                                                                          @Override
		                                                                          public void onEvent(List<RecentContact> messages) {
																					  System.err.println("message");
			                                                                          int index;
			                                                                          for (RecentContact msg : messages)
			                                                                          {
				                                                                          index = -1;
				                                                                          for (int i = 0; i < items.size(); i++)
				                                                                          {
					                                                                          if (msg.getContactId().equals(items.get(i).getContactId()) && msg.getSessionType() == (items.get(i).getSessionType()))
					                                                                          {
						                                                                          index = i;
						                                                                          break;
					                                                                          }
				                                                                          }
				                                                                          
				                                                                          if (index >= 0)
				                                                                          {
					                                                                          items.remove(index);
				                                                                          }
																						  items.add(msg);
																					  }
			                                                                          
			                                                                          refreshMessages(true);
			                                                                          
			                                                                          requestBuddyInfo(getRequestBuddyAccounts(messages));
		                                                                          }
	                                                                          };
	
	Observer<IMMessage>                         statusObserver                = new Observer<IMMessage>()
	                                                                          {
		                                                                          @Override
		                                                                          public void onEvent(IMMessage message) {
			                                                                          int index = getItemIndex(message.getUuid());
			                                                                          if (index >= 0 && index < items.size())
			                                                                          {
				                                                                          RecentContact item = items.get(index);
				                                                                          item.setMsgStatus(message.getStatus());
				                                                                          refreshViewHolderByIndex(index);
			                                                                          }
		                                                                          }
	                                                                          };
	
	Observer<RecentContact>                     deleteObserver                = new Observer<RecentContact>()
	                                                                          {
		                                                                          @Override
		                                                                          public void onEvent(RecentContact recentContact) {
			                                                                          if (recentContact != null)
			                                                                          {
				                                                                          for (RecentContact item : items)
				                                                                          {
					                                                                          if (TextUtils.equals(item.getContactId(), recentContact.getContactId()) && item.getSessionType() == recentContact.getSessionType())
					                                                                          {
						                                                                          items.remove(item);
						                                                                          refreshMessages(true);
						                                                                          break;
					                                                                          }
				                                                                          }
			                                                                          }
			                                                                          else
			                                                                          {
				                                                                          items.clear();
				                                                                          refreshMessages(true);
			                                                                          }
		                                                                          }
	                                                                          };
	
	TeamDataCache.TeamDataChangedObserver       teamDataChangedObserver       = new TeamDataCache.TeamDataChangedObserver()
	                                                                          {
		                                                                          
		                                                                          @Override
		                                                                          public void onUpdateTeams(List<Team> teams) {
			                                                                          adapter.notifyDataSetChanged();
		                                                                          }
		                                                                          
		                                                                          @Override
		                                                                          public void onRemoveTeam(Team team) {
			                                                                          //TODO 解散群
			                                                                          for (RecentContact item : items)
			                                                                          {
				                                                                          if (TextUtils.equals(item.getContactId(), team.getId()))
				                                                                          {
					                                                                          removeTeamInfo(item);
					                                                                          items.remove(item);
					                                                                          refreshMessages(true);
					                                                                          break;
				                                                                          }
			                                                                          }
		                                                                          }
	                                                                          };
	
	TeamDataCache.TeamMemberDataChangedObserver teamMemberDataChangedObserver = new TeamDataCache.TeamMemberDataChangedObserver()
	                                                                          {
		                                                                          @Override
		                                                                          public void onUpdateTeamMember(List<TeamMember> members) {
			                                                                          adapter.notifyDataSetChanged();
		                                                                          }
		                                                                          
		                                                                          @Override
		                                                                          public void onRemoveTeamMember(TeamMember member) {
			                                                                          
		                                                                          }
	                                                                          };
	
	ContactDataCache.BuddyDataChangedObserver   buddyDataChangedObserver      = new ContactDataCache.BuddyDataChangedObserver()
	                                                                          {
		                                                                          @Override
		                                                                          public void onUpdateBuddies(List<Buddy> buddies) {
			                                                                          adapter.notifyDataSetChanged();
		                                                                          }
	                                                                          };
	
	Observer<List<OnlineClient>>                clientsObserver               = new Observer<List<OnlineClient>>()
	                                                                          {
		                                                                          @Override
		                                                                          public void onEvent(List<OnlineClient> onlineClients) {
			                                                                          if (onlineClients == null || onlineClients.size() == 0)
			                                                                          {
				                                                                          notifyBar.setVisibility(View.GONE);
			                                                                          }
			                                                                          else
			                                                                          {
				                                                                          notifyBar.setVisibility(View.VISIBLE);
				                                                                          TextView status = (TextView) notifyBar.findViewById(R.id.status_desc_label);
				                                                                          OnlineClient client = onlineClients.get(0);
				                                                                          switch (client.getClientType()) {
																							  case ClientType.Windows:
																								  status.setText("Windows端在线");
																								  break;
																							  case ClientType.Web:
																								  status.setText("Web版在线");
																								  break;
																							  default:
																								  notifyBar.setVisibility(View.GONE);
																								  break;
																						  }
																					  }
																				  }
	                                                                          };
	
	protected void refreshViewHolderByIndex(final int index) {
		bActivity.runOnUiThread(new Runnable()
		{
			
			@Override
			public void run() {
				if (index < 0) { return; }
				
				Object tag = ListViewUtil.getViewHolderByIndex(listView, index);
				if (tag instanceof RecentViewHolder)
				{
					RecentViewHolder viewHolder = (RecentViewHolder) tag;
					viewHolder.refreshCurrentItem();
				}
			}
		});
	}
	
	/**
	 * 初始化消息列表
	 */
	private void initMessageList() {
		items = new ArrayList<RecentContact>();
		
		adapter = new TAdapter(context, items, this);
		listView.setAdapter(adapter);
		listView.setItemsCanFocus(true);
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				RecentContact item = (RecentContact) parent.getAdapter().getItem(position);
				if (item.getAttachment() != null && item.getAttachment() instanceof DismissAttachment)
				{
					Logger.e("当前群已经被解散了");
					Utils.showToast(context, "当前群已解散");
					removeTeamInfo(item);
					return;
				}
				talkto(item);
			}
		});
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
		{
			
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				if (position < listView.getHeaderViewsCount()) { return false; }
				showLongClickMenu((RecentContact) parent.getAdapter().getItem(position));
				
				return true;
			}
		});
		listView.setOnScrollListener(new OnScrollListener()
		{
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				adapter.onMutable(scrollState == SCROLL_STATE_FLING);
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				
			}
		});
	}
	
	protected void talkto(RecentContact item) {
		if (item == null) { return; }
		
		switch (item.getSessionType()) {
			case P2P:
				isFirstIn = 1;
				P2PMessageActivity.start(context, item.getContactId());
				break;
			case Team:
				isFirstIn = 1;
				TeamMessageActivity.start(context, item.getContactId());
				break;
			default:
				break;
		}
	}
	
	private void removeTeamInfo(RecentContact item) {
		NIMClient.getService(MsgService.class).deleteRecentContact2(item.getContactId(), item.getSessionType());
		NIMClient.getService(MsgService.class).clearChattingHistory(item.getContactId(), item.getSessionType());
		
	}
	
	private int getItemIndex(String uuid) {
		for (int i = 0; i < items.size(); i++)
		{
			RecentContact item = items.get(i);
			if (TextUtils.equals(item.getRecentMessageId(), uuid)) { return i; }
		}
		
		return -1;
	}
	
	private void showLongClickMenu(final RecentContact recent) {
		CustomAlertDialog alertDialog = new CustomAlertDialog(context);
		alertDialog.setTitle(MessageHelper.getName(recent.getContactId(), recent.getSessionType()));
		//		String title = getString(R.string.main_msg_list_delete_chatting);
		String title = context.getString(R.string.main_msg_list_delete_chatting);
		alertDialog.addItem(title, new onSeparateItemClickListener()
		{
			@Override
			public void onClick() {
				// 删除会话，删除后，消息历史被一起删除
				NIMClient.getService(MsgService.class).deleteRecentContact(recent);
				NIMClient.getService(MsgService.class).clearChattingHistory(recent.getContactId(), recent.getSessionType());
				items.remove(recent);
				ReminderManager.getInstance().updateSessionDeltaUnreadNum(-1 * recent.getUnreadCount());
				notifyDataSetChanged();
			}
		});
		
		title = ((recent.getTag() & RECENT_TAG_STICKY) == 0 ? context.getString(R.string.main_msg_list_sticky_on_top) : context.getString(R.string.main_msg_list_clear_sticky_on_top));
		alertDialog.addItem(title, new onSeparateItemClickListener()
		{
			@Override
			public void onClick() {
				if ((recent.getTag() & RECENT_TAG_STICKY) == 0)
				{
					addTag(recent, RECENT_TAG_STICKY);
				}
				else
				{
					removeTag(recent, RECENT_TAG_STICKY);
				}
				NIMClient.getService(MsgService.class).updateRecent(recent);
				
				refreshMessages(false);
			}
		});
		alertDialog.show();
	}
	
	private void addTag(RecentContact recent, long tag) {
		tag = recent.getTag() | tag;
		recent.setTag(tag);
	}
	
	private void removeTag(RecentContact recent, long tag) {
		tag = recent.getTag() & ~tag;
		recent.setTag(tag);
	}
	
	public void refreshMessages(boolean unreadChanged) {
		sortRecentContacts(items);
		notifyDataSetChanged();
		
		if (unreadChanged)
		{
			int unreadNum = 0;
			for (RecentContact r : items)
			{
				unreadNum += r.getUnreadCount();
			}
			ReminderManager.getInstance().updateSessionUnreadNum(unreadNum);
		}
	}
	
	private void sortRecentContacts(List<RecentContact> list) {
		if (list.size() == 0) { return; }
		Collections.sort(list, comp);
	}
	
	private static Comparator<RecentContact> comp = new Comparator<RecentContact>()
	                                              {
		                                              
		                                              @Override
		                                              public int compare(RecentContact o1, RecentContact o2) {
			                                              // 先比较置顶tag
			                                              long sticky = (o1.getTag() & RECENT_TAG_STICKY) - (o2.getTag() & RECENT_TAG_STICKY);
			                                              if (sticky != 0)
			                                              {
				                                              return sticky > 0 ? -1 : 1;
			                                              }
			                                              else
			                                              {
				                                              long time = o1.getTime() - o2.getTime();
				                                              return time == 0 ? 0 : (time > 0 ? -1 : 1);
			                                              }
		                                              }
	                                              };
	
	private void notifyDataSetChanged() {
		adapter.notifyDataSetChanged();
		boolean empty = items.isEmpty() && msgLoaded;
		emptyBg.setVisibility(empty ? View.VISIBLE : View.GONE);
		emptyHint.setHint("还没有会话，在通讯录中找个人聊聊吧！");
	}
	
	private Handler getHandler() {
		return handler;
	}
	
	private void postRunnable(final Runnable runnable) {
		handler.post(new Runnable()
		{
			@Override
			public void run() {
				// validate
				// TODO use getActivity ?
				//				if (!isAdded()) { return; }
				
				// run
				runnable.run();
			}
		});
	}
	
	private void postDelayed(final Runnable runnable, long delay) {
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run() {
				// validate
				// TODO use getActivity ?
				//				if (!isAdded()) { return; }
				
				// run
				runnable.run();
			}
		}, delay);
	}
	
	private void requestMessages(boolean delay) {
		if (msgLoaded) { return; }
		getHandler().postDelayed(new Runnable()
		{
			
			@Override
			public void run() {
				if (msgLoaded) { return; }
				
				NIMClient.getService(MsgService.class).queryRecentContacts().setCallback(new RequestCallback<List<RecentContact>>()
				{
					
					@Override
					public void onSuccess(List<RecentContact> recents) {
						//						loadedRecents = recents;
						
						// 需要请求群信息的群ID
						ArrayList<String> tids = new ArrayList<String>();
						ArrayList<String> pids = new ArrayList<String>();
						for (RecentContact r : recents)
						{
							if (r.getAttachment() != null && r.getAttachment() instanceof DismissAttachment)
							{
								Logger.e("当前群已经被解散了");
								removeTeamInfo(r);
							}
							else
							{
								items.add(r);
							}
							if (r.getSessionType() == SessionTypeEnum.Team && !TeamDataCache.getInstance().isTeamInCache(r.getContactId()))
							{
								tids.add(r.getContactId());
								if (TeamMemberInfoCache.getInstance().getTeamMember(r.getContactId()) == null)
								{
									qryTeamMember(r.getContactId());
								}
							}
							if (r.getSessionType() == SessionTypeEnum.P2P && TeamMemberInfoCache.getInstance().getTeammMemberInfo(r.getContactId()) == null)
							{
								pids.add(r.getContactId());
							}
						}
						qryStaffInfo(pids);
						
						if (tids.isEmpty())
						{
							msgLoaded = true;
							//							if (isAdded())
							//							{
							onRecentsLoaded();
							//							}
							
							return;
						}
						
						NIMClient.getService(TeamService.class).queryTeamListById(tids).setCallback(new RequestCallback<List<Team>>()
						{
							@Override
							public void onSuccess(List<Team> teams) {
								TeamDataCache.getInstance().addOrUpdateTeam(teams);
								
								msgLoaded = true;
								//								if (isAdded())
								//								{
								onRecentsLoaded();
								//								}
							}
							
							@Override
							public void onFailed(int code) {
								
							}
							
							@Override
							public void onException(Throwable exception) {
								
							}
						});
					}
					
					@Override
					public void onFailed(int code) {
					}
					
					@Override
					public void onException(Throwable exception) {
					}
				});
			}
		}, delay ? 250 : 0);
	}
	
	protected void onRecentsLoaded() {
		//		items.clear();
		//		if (loadedRecents != null)
		if (items != null)
		{
			//			items.addAll(loadedRecents);
			refreshMessages(true);
			requestBuddyInfo(getRequestBuddyAccounts(items));
		}
		else
		{
			refreshMessages(false);
		}
	}
	
	private void qryStaffInfo(ArrayList<String> pids) {
		if (null == pids || pids.isEmpty()) return;
		ActionHelp.qryStaffInfoByAccIds(context, pids.toString(), new ObjectHttpResponseHandler<List<TeamMemberX>>()
		{
			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				String errorMsg = "request team member list failed :" + statusCode;
				//				LogUtil.e(TAG, errorMsg);
			}
			
			@Override
			public void onSuccess(int statusCode, Header[] headers, List<TeamMemberX> object, String responseString) {
				if (object != null && !object.isEmpty())
				{
					TeamMemberInfoCache.getInstance().add(object);
					refreshMessages(false);
				}
			}
			
			@Override
			public Type getType() {
				return new TypeToken<List<TeamMemberX>>()
				{
				}.getType();
			}
		});
	}
	
	private void qryTeamMember(final String teamId) {
		ActionHelp.qryMemberListByTeamId(context, teamId, new ObjectHttpResponseHandler<List<TeamMemberX>>()
		{
			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			}
			
			@Override
			public void onSuccess(int statusCode, Header[] headers, List<TeamMemberX> object, String responseString) {
				if (object != null && !object.isEmpty())
				{
					TeamMemberInfoCache.getInstance().addTeam(teamId, object);
					TeamMemberInfoCache.getInstance().add(object);
				}
			}
			
			@Override
			public Type getType() {
				return new TypeToken<List<TeamMemberX>>()
				{
				}.getType();
			}
		});
	}
	
	private List<String> getRequestBuddyAccounts(final List<RecentContact> recentContacts) {
		List<String> accounts = new ArrayList<String>();
		for (RecentContact r : recentContacts)
		{
			if (r.getFromAccount() != null && r.getSessionType() == SessionTypeEnum.P2P && !r.getFromAccount().equals(NimCache.getAccount()) && !ContactDataCache.getInstance().hasBuddy(r.getFromAccount()))
			{
				accounts.add(r.getFromAccount());
			}
		}
		
		return accounts;
	}
	
	private void requestBuddyInfo(List<String> accounts) {
		if (accounts == null || accounts.isEmpty()) { return; }
		
		//		Log.i(TAG, "MessageList request buddy info, accounts:" + accounts.toString());
		ContactHttpClient.getInstance().getUserInfo(accounts, new IContactHttpCallback<List<Buddy>>()
		{
			@Override
			public void onSuccess(List<Buddy> buddies) {
				ContactDataCache.getInstance().addOrUpdateBuddies(buddies);
				notifyDataSetChanged();
			}
			
			@Override
			public void onFailed(int code, String errorMsg) {
				//				emptyBg.setVisibility(View.VISIBLE);
				//				emptyHint.setHint("还没有会话，在通讯录中找个人聊聊吧！");
			}
		});
	}
	
	@Override
	public int enter(IUIPage<?> arg0) {
		if (isFirstIn == 1)
		{
			//		initViews();
			//		initMessageList();
			msgLoaded = false;
			items.clear();
			requestMessages(true);
			
			//		registerObservers(true);
		}
		//		isFirstIn = 0;
		//		if (isFirstIn == 2)
		//		{
		//			msgLoaded = false;
		//			requestMessages(true);
		//		}
		return 0;
	}
	
	@Override
	public int leave(IUIPage<?> arg0) {
		registerObservers(false);
		return 0;
	}
	
	@Override
	public void notifyView(int arg0, Bundle arg1) {
		
	}
	
	@Override
	public void setStatus(int arg0) {
		
	}
	
	@Override
	public View updateView(int arg0, ITaskResult arg1) {
		return null;
	}
	
	@Override
	public int getViewTypeCount() {
		return 3;
	}
	
	@Override
	public Class<? extends TViewHolder> viewHolderAtPosition(int position) {
		SessionTypeEnum type = items.get(position).getSessionType();
		
		switch (type) {
			case P2P:
				return IMRecentViewHolder.class;
			case Team:
				return TeamRecentViewHolder.class;
			default:
				return CommonRecentViewHolder.class;
		}
	}
	
	@Override
	public boolean enabled(int position) {
		return true;
	}
	
}
